﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using E.Model;
using System.Data;
using System.Data.SqlClient;
using X.DALSqlServer;

namespace E.DAL
{
    public class ReportDAL
    {
        /// <summary>
        /// 统计课件学习进度,课时完成情况
        /// </summary>
        /// <param name="searchModel"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public ReportStudyInfoStaticsModel SearchReportStudyInfo(SearchReportStudyInfoModel searchModel, int pageSize, int pageIndex)
        {
            ReportStudyInfoStaticsModel listModel = new ReportStudyInfoStaticsModel();
            IDataParameter[] parms = new IDataParameter[] { 
                new SqlParameter("@CourseID", searchModel.CourseID),
                new SqlParameter("@PageSize", pageSize),
                new SqlParameter("@PageIndex", pageIndex)
            };
            IList<ReportStudyInfoModel> list = new List<ReportStudyInfoModel>();
            using (IDataReader dr = SqlHelper.ExecuteDataReaderByStoredProc("usp_getStudentStudyInfo", parms))
            {
                int totalCount = 0;
                while (dr.Read())
                {
                    if (dr["ID"] == DBNull.Value)
                    {
                        totalCount = Convert.ToInt32(dr["pos"]);
                    }
                    else
                    {
                        ReportStudyInfoModel ssm = new ReportStudyInfoModel();
                        ssm.LoginID = dr["LoginID"].ToString();
                        ssm.UserName = dr["UserName"].ToString();
                        ssm.CourseName = dr["CourseName"].ToString();
                        ssm.Process = (int)dr["Process"];
                        list.Add(ssm);
                    }
                }
                listModel.TotalCount = totalCount;
                listModel.List = list;
                return listModel;
            }
        }


        /// <summary>
        /// 统计学生登录情况及次数
        /// </summary>
        /// <param name="searchModel"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public ReportStudyInfoStaticsModel SearchReportLoginInfo(SearchReportStudyInfoModel searchModel, int pageSize, int pageIndex)
        {
            ReportStudyInfoStaticsModel listModel = new ReportStudyInfoStaticsModel();
            IDataParameter[] parms = new IDataParameter[] { 
                new SqlParameter("@FromDate", searchModel.FromDate),
                new SqlParameter("@ToDate", searchModel.ToDate),
                new SqlParameter("@PageSize", pageSize),
                new SqlParameter("@PageIndex", pageIndex)
            };
            IList<ReportStudyInfoModel> list = new List<ReportStudyInfoModel>();
            using (IDataReader dr = SqlHelper.ExecuteDataReaderByStoredProc("usp_getStudentLoginInfo", parms))
            {
                int totalCount = 0;
                while (dr.Read())
                {
                    if (dr["ID"] == DBNull.Value)
                    {
                        totalCount = Convert.ToInt32(dr["pos"]);
                    }
                    else
                    {
                        ReportStudyInfoModel ssm = new ReportStudyInfoModel();
                        ssm.LoginID = dr["LoginID"].ToString();
                        ssm.UserName = dr["UserName"].ToString();
                        ssm.TotalLoginCount = (int)dr["TotalLoginNumbers"];
                        ssm.LastLoginTime = dr["LastLoginTime"].ToString();
                        list.Add(ssm);
                    }
                }
                listModel.TotalCount = totalCount;
                listModel.List = list;
                return listModel;
            }
        }
    }
}
